Skip to content

Reuse buffers in ServerMessage<BsatnFormat> #2911

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: centril/execute-plan-no-temp-vecs
Choose a base branch
from

Conversation

Centril
Copy link
Contributor

@Centril Centril commented Jul 2, 2025

Description of Changes

Fixes #2824.

Defines a global pool BsatnRowListBuilderPool which reclaims the buffers of a ServerMessage<BsatnFormat> and which is then used when building new ServerMessage<BsatnFormat>s.

Notes:

  1. The new pool BsatnRowListBuilderPool reports the same kind of metrics to prometheus as PagePool does.
  2. BsatnRowListBuilder now works in terms of BytesMut.
  3. The trait method fn to_bsatn_extend is redefined to be capable of dealing with BytesMut as well as Vec<u8>.
  4. A trait ConsumeEachBuffer is defined from ServerMessage<BsatnFormat> and down to extract buffers. <ServerMessage<_> as ConsumeEachBuffer>::consume_each_buffer(...) is then called in messages::serialize(...) just after bsatn-encoding the entire message and before any compression is done. This is the place where the pool reclaims buffers.

Benchmarks

Benchmark numbers vs. master using cargo bench --bench subscription -- --baseline subs on i7-7700K, 64GB RAM:

footprint-scan          time:   [21.607 ms 21.873 ms 22.187 ms]
                        change: [-62.090% -61.438% -60.787%] (p = 0.00 < 0.05)
                        Performance has improved.

full-scan               time:   [22.185 ms 22.245 ms 22.324 ms]
                        change: [-36.884% -36.497% -36.166%] (p = 0.00 < 0.05)
                        Performance has improved.

The improvements in footprint-scan are mostly thanks to #2918, but 7 ms of the improvements here are thanks to the pool. The improvements to full-scan should be only thanks to the pool.

API and ABI breaking changes

None

Expected complexity level and risk

2?

Testing

  • Tests for Pool<T> also apply to BsatnRowListBuilderPool.

@Centril Centril force-pushed the centril/subs-less-alloc branch 4 times, most recently from f4b0fb9 to ed80830 Compare July 2, 2025 14:40
@Centril Centril marked this pull request as ready for review July 3, 2025 09:53
@Centril Centril requested a review from gefjon as a code owner July 3, 2025 09:53
@joshua-spacetime
Copy link
Collaborator

Can you make the first commit its own independent PR?

@Centril
Copy link
Contributor Author

Centril commented Jul 3, 2025

Can you make the first commit its own independent PR?

Will do 👍

@Centril Centril force-pushed the centril/subs-less-alloc branch 3 times, most recently from 0e9622d to 6b90886 Compare July 4, 2025 09:04
@Centril Centril changed the base branch from master to centril/execute-plan-no-temp-vecs July 4, 2025 09:04
@Centril Centril force-pushed the centril/subs-less-alloc branch 3 times, most recently from 5f2394e to b961e78 Compare July 4, 2025 09:48
@Centril Centril force-pushed the centril/subs-less-alloc branch from b961e78 to bbeec32 Compare July 4, 2025 10:06
@Centril Centril changed the base branch from centril/execute-plan-no-temp-vecs to centril/object-pool July 4, 2025 10:06
@Centril Centril force-pushed the centril/subs-less-alloc branch 3 times, most recently from a7aa61a to b0cc2d4 Compare July 4, 2025 11:47
@Centril Centril force-pushed the centril/object-pool branch 2 times, most recently from fa008fb to 9411e5e Compare July 4, 2025 13:27
Base automatically changed from centril/object-pool to master July 4, 2025 14:14
@Centril Centril force-pushed the centril/subs-less-alloc branch from b0cc2d4 to f3be26f Compare July 4, 2025 14:20
@Centril Centril force-pushed the centril/subs-less-alloc branch from f3be26f to f112436 Compare July 4, 2025 14:21
@Centril Centril changed the base branch from master to centril/execute-plan-no-temp-vecs July 4, 2025 14:21
@Centril
Copy link
Contributor Author

Centril commented Jul 4, 2025

This is now a fairly small PR based atop another fairly small PR github.com//pull/2918.

@Centril Centril force-pushed the centril/subs-less-alloc branch from f112436 to 88cb22c Compare July 4, 2025 17:51
Copy link
Contributor

@gefjon gefjon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved as a code owner, and no objections otherwise, though I'd still like review from @joshua-spacetime re: the metrics.

@bfops bfops added release-any To be landed in any release window performance A PR/Issue related to improving performance of stdb labels Jul 7, 2025
Copy link
Collaborator

@joshua-spacetime joshua-spacetime left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we wait for #2866 to land first, this doesn't have to be a pool, it can just be a thread local buffer. I think I'd prefer we wait for that in order to simplify things even further.

@Centril
Copy link
Contributor Author

Centril commented Jul 8, 2025

If we wait for #2866 to land first, this doesn't have to be a pool, it can just be a thread local buffer. I think I'd prefer we wait for that in order to simplify things even further.

For a thread local to work, you'd need to move messages::serialize, which reclaims the buffers and re-adds them to the pool, to the same thread as does the query evaluation. Currently, #2866 doesn't do that, and it doesn't seem like that would be profitable.

@Centril Centril requested review from joshua-spacetime and removed request for cloutiertyler July 8, 2025 09:19
@Centril Centril force-pushed the centril/execute-plan-no-temp-vecs branch from 6271604 to c134d82 Compare July 15, 2025 17:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance A PR/Issue related to improving performance of stdb release-any To be landed in any release window
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add buffer pool for serializing subscription results
4 participants